package com.qianniu.napi.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class CheckUtil {
    private final static Logger logger = LoggerFactory.getLogger(CheckUtil.class);

    private static String MSG_SEC_CHECK_URL = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=ACCESS_TOKEN";
    private static String IMG_SEC_CHECK_URL = "https://api.weixin.qq.com/wxa/img_sec_check?access_token=ACCESS_TOKEN";


    public static boolean checkMessage(String msg, String mpid) {
        Boolean flag = true;
        String url = MSG_SEC_CHECK_URL.replace("ACCESS_TOKEN", WeixinUtil.getAccessToken(mpid));
        logger.debug("-------------------------url=" + url);
        JSONObject param = new JSONObject();
        param.put("content", msg);

        String result = HttpUtils.post(url, param);

        logger.debug("-------------------------result=" + result);
        com.alibaba.fastjson.JSONObject jo = JSON.parseObject(result);

        return jo.getInteger("errcode") == 0;
    }

    public static boolean checkImage(String file, String mpid) {

        String url = IMG_SEC_CHECK_URL.replace("ACCESS_TOKEN", WeixinUtil.getAccessToken(mpid));
        logger.debug("-------------------------url=" + url);
        File srcfile = new File(file);
        if (!srcfile.exists()) return false;

        if (!srcfile.exists()) return false;

        String result = HttpUtils.postFile(url, "media", file, null, null);

        logger.debug("-------------------------result=" + result);
        JSONObject jo = JSON.parseObject(result);

        return jo.getInteger("errcode") == 0;
    }

}
